package com.sanpu.kxe.business.member.view;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.member.adapter.MemberLevelsListAdapter;
import com.sanpu.kxe.business.member.bean.MemberLevelOutput;
import com.sanpu.kxe.business.member.contract.MemberLevelsListContract;
import com.sanpu.kxe.business.member.presenter.MemberLevelsListPresenter;
import com.sanpu.kxe.business.member.provider.MemberOpenProvider;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.module.widget.VerticalSwipeRefreshLayout;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Description：会员等级列表
 * Created by 薛勇军 on 2018/6/12.
 */
@Route(path = "/member/levelsListAct")
public class MemberLevelsListActivity extends BaseTitleActivity implements MemberLevelsListContract.View {

    @Bind(R.id.list_view)
    RecyclerView vListView;
    @Bind(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;

    private LinearLayout productLl;

    private MemberLevelsListAdapter levelsListAdapter;

    private MemberLevelsListContract.Presenter mPresenter;

    private int mCurrentLvl;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_member_levels_list;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("会员升级");
        mPresenter = new MemberLevelsListPresenter(this);
        View footView = LayoutInflater.from(mContext).inflate(R.layout.view_member_levels_foot, null);
        productLl = ButterKnife.findById(footView, R.id.productLl);
        levelsListAdapter = new MemberLevelsListAdapter();
        levelsListAdapter.addFooterView(footView);
        vListView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        levelsListAdapter.setEnableLoadMore(false);
        vListView.setAdapter(levelsListAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        final UserCenterOutput centerOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (centerOutput != null) {
            mCurrentLvl = centerOutput.getLevelId();
        }
        mPresenter.findVipConfList();
    }

    @Override
    protected void setListener() {
        super.setListener();
        productLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MemberOpenProvider.getInstance().openMemberProductRateActivity(mContext);
            }
        });
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.findVipConfList();
            }
        });
    }

    @Override
    public void showMemberList(List<MemberLevelOutput> memberLevelOutputs) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        if (memberLevelOutputs != null && memberLevelOutputs.size()>0) {
            levelsListAdapter.setData(mCurrentLvl, mPresenter.getLevelListener());
            levelsListAdapter.setNewData(memberLevelOutputs);
            levelsListAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(MemberLevelsListContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
